package com.citcc.web.controller.business;

import com.citcc.business.bean.CdzRates;
import com.citcc.business.bean.CdzStation;
import com.citcc.business.service.CdzRatesService;
import com.citcc.business.service.CdzStakeService;
import com.citcc.business.service.CdzStationService;
import com.citcc.common.annotation.Log;
import com.citcc.common.base.AjaxResult;
import com.citcc.common.enums.BusinessType;
import com.citcc.common.page.TableDataInfo;
import com.citcc.common.utils.StringUtils;
import com.citcc.framework.util.ShiroUtils;
import com.citcc.framework.web.base.BaseController;
import com.citcc.web.controller.util.MapDistanceUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/cdzStation")
public class CdzStationController extends BaseController {

    private String prefix = "business/cdzStation";

    @Autowired
    private CdzStationService cdzStationService;
    @Autowired
    private CdzRatesService cdzRatesService;
    @Autowired
    private CdzStakeService cdzStakeService;

    @RequiresPermissions("business:cdzStation:view")
    @GetMapping("/main")
    public String vehicle(ModelMap map){
        CdzStation station = new CdzStation();
        station.setOrgId(getSysUser().getDeptId());
        map.put("station",cdzStationService.selectCdzStationAll(station));
        return prefix + "/main";
    }

    @RequiresPermissions("business:cdzStation:list")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(CdzStation station){
        startPage();
        if (station.getOrgId()==null||"".equals(station.getOrgId())) {
            station.setOrgId(getSysUser().getDeptId());
        }
        List<CdzStation> list=cdzStationService.selectCdzStationAll(station);
        return getDataTable(list);
    }
    @GetMapping("/add")
    public String toAdd(ModelMap map)
    {
        CdzRates rates=new CdzRates();
        rates.setOrgId(getSysUser().getDeptId());
        map.put("rateList",cdzRatesService.selectAllList(rates));
        return prefix + "/add";
    }

    @RequiresPermissions("business:cdzStation:add")
    @Log(title = "充电站管理", businessType = BusinessType.INSERT)
    @PostMapping("/addSave")
    @ResponseBody
    public AjaxResult addSave(CdzStation station)
    {
        if (station.getOrgId()==null||"".equals(station.getOrgId())) {
            station.setOrgId(getSysUser().getDeptId());
        }
        station.setCreateBy(ShiroUtils.getLoginName());
        station.setDelFlag("0");
        if(StringUtils.isNotEmpty(station.getLon())&&StringUtils.isNotEmpty(station.getLat())){
            String address=MapDistanceUtil.getLocationAddr(station.getLon()+","+station.getLat());
            station.setSite(address);
        }
        return toAjax(cdzStationService.insert(station));
    }

    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id")String id, ModelMap map)
    {
        CdzRates rates=new CdzRates();
        rates.setOrgId(getSysUser().getDeptId());
        map.put("rateList",cdzRatesService.selectAllList(rates));
        map.put("station",cdzStationService.getCdzStation(id));
        return prefix + "/edit";
    }


    @RequiresPermissions("business:cdzStation:edit")
    @Log(title = "充电站管理", businessType = BusinessType.UPDATE)
    @PostMapping("/editSave")
    @ResponseBody
    public AjaxResult editSave(CdzStation station)
    {
        station.setUpdateBy(ShiroUtils.getLoginName());
        station.setUpdateTime(new Date());
        if(StringUtils.isNotEmpty(station.getLon())&&StringUtils.isNotEmpty(station.getLat())){
            String address=MapDistanceUtil.getLocationAddr(station.getLon()+","+station.getLat());
            station.setSite(address);
        }
        return toAjax(cdzStationService.update(station));
    }

    @RequiresPermissions("business:cdzStation:remove")
    @Log(title = "充电站管理", businessType = BusinessType.UPDATE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String id)
    {
        try
        {
            int count=cdzStakeService.countStation(id);
            if(count>0){
                return error("该充电站有充电桩不能删除");
            }else{
                Integer e =cdzStationService.delete(id);
                return toAjax(e);
            }
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }
}
